# 作者： 李泉志
# 2025年01月10日21时01分秒
# 2947029073@qq.com

# 1、练习封装案例
class thing:
    def __init__(self, name, area):
        self.name = name
        self.area = area

    def __str__(self):
        return "Thing(name=%s, area=%d)" % (self.name, self.area)

    def use(self):
        print("使用%s..." % self.name)


class Table:
    def __init__(self, width, length):
        self.width = width
        self.length = length
        self.area = self.width * self.length
        self.things = []

    def __str__(self):
        return "Table(width=%d, length=%d, area=%d)" % (self.width, self.length, self.area)

    def add_thing(self, item: thing):
        if item.area > self.area:
            print("不能放这么大的东西...")
            return
        else:
            self.things.append(item)
            self.area -= item.area
            print("成功放置%s, 剩余空间%d" % (item.name, self.area))

    def use_things(self):
        for item in self.things:
            item.use()


if __name__ == '__main__':
    pencil = thing("铅笔", 1)
    eraser = thing("橡皮擦", 2)
    ruler = thing("尺子", 3)
    table = Table(10, 10)
    print(table)
    print(pencil)
    table.add_thing(pencil)
    table.add_thing(eraser)
    table.add_thing(ruler)
    print(table)
    table.use_things()
